package com.ecy.quadratic.ui.activity;

/**
 * 关于火星兔子
 *
 * @author guofu
 */

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.ecy.quadratic.R;
import com.ecy.quadratic.reader.base.BaseActivity;
import com.ecy.quadratic.reader.component.AppComponent;

import butterknife.Bind;

public class VideoPlayActivity extends BaseActivity {
    @Bind(R.id.details_webview)
    WebView mWebView;
    @Bind(R.id.webprogressbar)
    ProgressBar progressBar;
    String mUrl = "http://www.marsooo.com";
    private WebSettings mWebSettigs;

    public static final void startActivity(Context context, String url) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_videoplay;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    public void initToolBar() {

    }

    @Override
    public void initData() {
        mUrl = getIntent().getStringExtra("url");
    }

    @Override
    public void configViews() {
        mWebSettigs = mWebView.getSettings();
        mWebSettigs.setBuiltInZoomControls(false);
        mWebSettigs.setDomStorageEnabled(true);
        mWebSettigs.setJavaScriptEnabled(true);
//		mWebSettigs.setBlockNetworkImage(true);
//		mWebSettigs.setBlockNetworkLoads(true);
        mWebView.loadUrl(mUrl);

        //设置不用系统浏览器打开,直接显示在当前Webview
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //设置WebChromeClient类
        mWebView.setWebChromeClient(new WebChromeClient() {


            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
            }


            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
//				if(newProgress == 100) {
//					progressBar.setVisibility(View.GONE);
//				} else {
//					if (progressBar == null) {
//						return;
//					}
//					if(View.GONE == progressBar.getVisibility()) {
//						progressBar.setVisibility(View.VISIBLE);
//					}
//					progressBar.setProgress(newProgress);
//				}
                super.onProgressChanged(view, newProgress);
            }
        });


        //设置WebViewClient类
        mWebView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
//				mWebSettigs.setBlockNetworkImage(false);
//				mWebSettigs.setBlockNetworkLoads(false);
            }
        });
    }

}
